﻿using Furion;
using Kevin.Code.Generator.DbAccessor.Models;
using SqlSugar;
using Sunny.UI;

namespace Kevin.Code.Generator.Pages
{
    public partial class CodeGeneratePage : UIPage
    {
        private DB_Connection DbLink;

        private readonly SqlSugarClient _repository;


        public CodeGeneratePage(DB_Connection dbLink)
        {
            this.DbLink = dbLink;
            _repository = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = dbLink.Connection_String,
                DbType = (DbType)dbLink.Type,
                IsAutoCloseConnection = true
            });
            InitializeComponent();

            dbColumnList.AddColumn("字段名称", "ColumnName");
            dbColumnList.AddColumn("列表展示复选框", "ListShowCheckBox");
            dbColumnList.AddColumn("列表展示", "ListShow");
            dbColumnList.AddColumn("添加展示", "InsertShow");
            //SunnyUI常用的初始化配置，看个人喜好用或者不用。
            dbColumnList.Init();
        }

        private void CodeGeneratePage_Initialize(object sender, EventArgs e)
        {
            this.dbNameLabel.Text = DbLink.Name;
            this.dbTypeLabel.Text = ((DbType)DbLink.Type).ToString();
            var allTables = _repository.DbMaintenance.GetTableInfoList(false);
            if (allTables != null && allTables.Count > 0)
            {
                foreach (var table in allTables)
                    this.dbTableSelect.Items.Add(table.Name);
            }
        }

        private void dbTableSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            var tableName = this.dbTableSelect.Text;
            if (string.IsNullOrEmpty(tableName))
                return;
            var columns = _repository.DbMaintenance.GetColumnInfosByTableName(tableName, false);
            if (columns != null && columns.Count > 0)
            { 
                //var datas = columns.Select()
            }
        }
    }
}
